package gui;

import java.awt.Point;

import org.eclipse.swt.graphics.FontMetrics;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Button;
import org.eclipse.wb.swt.SWTResourceManager;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;

/**
 * Provides a simple parameterized dialog box.
 * 
 * Each simpleDialog has a title, body, and dimensions which are passed into the constructor and then used to create a window.
 * 
 * TODO: Add public static methods for determining font dimensions for automatic window sizing, regardless of language.
 * @author Lane Aasen
 */
public class SimpleDialog extends Dialog {

	protected Object result;
	protected Shell shlSimpleDialog;
	private String body;
	private int width;
	private int height;
	private int border;

	/**
	 * Constructs a new simple dialog object with minimal information.
	 * 
	 * @param parent
	 * @param style
	 * @param title
	 * @param body
	 * @param width
	 * @param height
	 */
	public SimpleDialog(Shell parent, int style, String title, String body, int width, int height, int border) {
		super(parent, style);
		setText(title);
		this.body = body;
		this.width = width;
		this.height = height;
		this.border = border;
	}

	/**
	 * Open the dialog.
	 * 
	 * @return the result
	 */
	public Object open() {
		createContents();
		shlSimpleDialog.open();
		shlSimpleDialog.layout();
		Display display = getParent().getDisplay();
		while (!shlSimpleDialog.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
		return result;
	}

	/**
	 * Create contents of the dialog.
	 */
	private void createContents() {
		shlSimpleDialog = new Shell(getParent(), getStyle());
		shlSimpleDialog.setText(getText());
		
		Label lbl = new Label(shlSimpleDialog, SWT.NONE);
		lbl.setText(this.body);
		org.eclipse.swt.graphics.Point lblOptimalSize = lbl.computeSize(SWT.DEFAULT, SWT.DEFAULT);
		lbl.setBounds(border, border, lblOptimalSize.x, lblOptimalSize.y);
		
		Button btnClose = new Button(shlSimpleDialog, SWT.NONE);
		btnClose.setText("Close");
		org.eclipse.swt.graphics.Point btnOptimalSize = btnClose.computeSize(SWT.DEFAULT, SWT.DEFAULT);
		Rectangle lblBounds = lbl.getBounds();
		btnClose.setBounds(lblBounds.x + lblBounds.width - btnOptimalSize.x, lblBounds.y + lblBounds.height + border, btnOptimalSize.x, btnOptimalSize.y);
		btnClose.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent arg0) {
				shlSimpleDialog.close();
			}
		});
		
		//Rectangle btnBounds = btnClose.getBounds();
		org.eclipse.swt.graphics.Point shlOptimalSize = shlSimpleDialog.computeSize(SWT.DEFAULT, SWT.DEFAULT);
		shlSimpleDialog.setSize(shlOptimalSize.x + border, shlOptimalSize.y + border);
	}
}
